import { _decorator, Component, find, instantiate, Node } from 'cc';
import { ResMgr } from './ResMgr';
const { ccclass, property } = _decorator;

@ccclass('UIMgr')
/**
 * 场景管理
 */
export class UIMgr extends Component {

    //
    public static Instance: UIMgr = null;

    protected onLoad(): void {
        UIMgr.Instance = this;
    }

    //游戏主要层级
    private MainLoad: Node = null;

    //当前游戏场景
    public Scene: Node = null;

    //游戏弹窗层级
    private PopLoad: Node = null;
    private PopMap: Map<String, Node> = new Map();

    //游戏顶部层级
    private TopLoad: Node = null;
    private TopMap: Map<String, Node> = new Map();

    protected start(): void {
        this.MainLoad = find("MainLoad", this.node);
        this.PopLoad = find("PopLoad", this.node);
        this.Scene = find("MainLoad/Loading", this.node);
        this.TopLoad = find("TopLoad", this.node);
    }

    /**
     * 更新游戏场景
     */
    public UdpateScene(_scene: string) {
        if (this.Scene) this.Scene.parent && this.Scene.parent.removeChild(this.Scene);
        this.Scene = instantiate(ResMgr.Instance.Prefabs[_scene]);
        this.MainLoad.addChild(this.Scene);
    }

    /**
     * 显示弹窗
     */
    public ShowPop(popName: string) {
        //
        if (this.PopMap.has(popName)) {
            this.PopMap.get(popName).active = true;
            this.PopMap.get(popName).setSiblingIndex(100);
            return this.PopMap.get(popName);
        }
        //
        let pop = instantiate(ResMgr.Instance.Prefabs[popName]);
        this.PopLoad.addChild(pop);
        this.PopMap.set(popName, pop);
        return this.PopMap.get(popName);
    }

    /**
     * 隐藏弹窗
     */
    public HidePop(popName: string) {
        this.PopMap.get(popName).active = false;
        return this.PopMap.get(popName);
    }

    /**
    * 显示弹窗
    */
    public ShowTop(_topname: string) {
        //
        if (this.TopMap.has(_topname)) {
            this.TopMap.get(_topname).active = true;
            this.TopMap.get(_topname).setSiblingIndex(100);
            return this.TopMap.get(_topname);
        }
        //
        let pop = instantiate(ResMgr.Instance.Prefabs[_topname]);
        this.TopLoad.addChild(pop);
        this.TopMap.set(_topname, pop);
        return this.TopMap.get(_topname);
    }

    /**
     * 隐藏弹窗
     */
    public HideTop(_topname: string) {
        this.TopMap.get(_topname).active = false;
        return this.TopMap.get(_topname);
    }

}


